package cn.wnhyang.generator.controller;

import cn.wnhyang.generator.convert.AssemblyLogConvert;
import cn.wnhyang.generator.pojo.CommonResult;
import cn.wnhyang.generator.service.AssemblyLogService;
import cn.wnhyang.generator.util.ExcelUtils;
import cn.wnhyang.generator.vo.AssemblyLogVO;
import cn.wnhyang.generator.vo.create.AssemblyLogCreateVO;
import cn.wnhyang.generator.vo.page.AssemblyLogPageVO;
import cn.wnhyang.generator.vo.update.AssemblyLogUpdateVO;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import cn.wnhyang.generator.pojo.PageResult;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

import static cn.wnhyang.generator.pojo.CommonResult.success;

/**
 * 单柜装配日志
 *
 * @author cfl
 * @since 2024/11/26
 */
@Slf4j
@RestController
@RequestMapping("/assemblyLog")
@RequiredArgsConstructor
public class AssemblyLogController {

    private final AssemblyLogService assemblyLogService;
    
    /**
     * 新增
     *
     * @param createVO 创建VO
     * @return id
     */
    @PostMapping
    public CommonResult<Long> create(@RequestBody @Valid AssemblyLogCreateVO createVO) {
        return success(assemblyLogService.create(createVO));
    }
    
    /**
     * 更新
     *
     * @param updateVO 更新VO
     * @return true/false
     */
    @PutMapping
    public CommonResult<Boolean> update(@RequestBody @Valid AssemblyLogUpdateVO updateVO) {
        assemblyLogService.update(updateVO);
        return success(true);
    }
    
    /**
     * 删除
     *
     * @param id id
     * @return true/false
     */
    @DeleteMapping
    public CommonResult<Boolean> delete(@RequestParam("id") Long id) {
        assemblyLogService.delete(id);
        return success(true);
    }
    
    /**
     * 查询单个
     *
     * @param id id
     * @return vo
     */
    @GetMapping("/{id}")
    public CommonResult<AssemblyLogVO> get(@PathVariable("id") Long id) {
        return success(AssemblyLogConvert.INSTANCE.convert(assemblyLogService.get(id)));
    }
    
    /**
     * 分页查询
     *
     * @param pageVO 分页VO
     * @return pageResult
     */
    @GetMapping("/page")
    public CommonResult<PageResult<AssemblyLogVO>> page(@Valid AssemblyLogPageVO pageVO) {
        return success(AssemblyLogConvert.INSTANCE.convert(assemblyLogService.page(pageVO)));
    }

    /**
    * 导出
    *
    * @param pageVO 导出VO
    * @param response response
    * @throws IOException IO异常
    */
    @GetMapping("/export")
    public void exportExcel(@Valid AssemblyLogPageVO pageVO, HttpServletResponse response) throws IOException {
        // 输出 Excel
        ExcelUtils.write(response, "AssemblyLogVO.xls", "数据", AssemblyLogVO.class, AssemblyLogConvert.INSTANCE.convert(assemblyLogService.page(pageVO)).getList());
    }

    /**
     * 导入
     *
     * @param file 文件
     * @return 结果
     * @throws IOException IO异常
     */
    @PostMapping("/import")
    public CommonResult<Boolean> importExcel(@RequestParam("file") MultipartFile file) throws IOException {
        List<AssemblyLogVO> read = ExcelUtils.read(file, AssemblyLogVO.class);
        // do something
        return success(true);
    }
}
